<%
  license = Avo.license
  information_items = [
    'license_key',
    'ruby_version',
    'rails_version',
    'environment',
    'host',
    'port',
    'ip',
    'app_name',
  ].excluding(Avo.configuration.exclude_from_status)

  hq_payload = Avo::Licensing::HQ.new(request).payload
%>
<div class="flex flex-col">
  <%= render Avo::PanelComponent.new(name: 'Avo Status', description: 'Use this page to debug your Avo instance.') do |c| %>
    <% c.with_tools do %>
    <% end %>
    <% c.with_bare_content do %>
      <div class="grid gap-4 grid-cols-2">
        <div class="relative flex flex-col bg-white rounded shadow-panel p-4 space-y-4 h-full">
          <div class="flex justify-between w-full">
            <div class="font-semibold">License info</div>
            <div class="flex justify-self-end">
              <%= a_button style: :outline,
                color: :blue,
                url: "#{root_path}avo_private/debug/refresh_license",
                method: :post,
                loading: true,
                size: :xs,
                icon: 'heroicons/outline/refresh' do %>
                Refresh license
              <% end %>
            </div>
          </div>
          <div class="flex flex-col flex-1">
            <div>
              <div class="text-xl font-semibold"><%= render partial: "valid_indicator", locals: {valid: license.valid?} %> <%= license.name %> license </div>
              <% if license.response['reason'] %>
                <dl>
                  <dt class="font-semibold text-sm">Error</dt>
                  <dd class="text-red-600"><%= license.response['reason'] %></dd>
                </dl>
              <% end %>
              <% if license.response['error'] %>
                <dl>
                  <dt class="font-semibold text-sm">Error</dt>
                  <dd class="text-red-600"><%= license.response['error'] %></dd>
                  <dt class="font-semibold text-sm">Exception message</dt>
                  <dd class="text-red-600"><%= license.response['exception_message'] %></dd>
                </dl>
              <% end %>
              <div class="mt-4">
                <dl>
                  <% information_items.each do |item| %>
                    <dt class="font-semibold text-sm"><%= item.humanize %></dt>
                    <dd><%= license.response[item] %></dd>
                  <% end %>
                  <dt class="font-semibold text-sm">Last fetched at</dt>
                  <dd><%= license.response['fetched_at'] %> <br>
                    <% if license.response['fetched_at'].present? %>
                      (<%= time_ago_in_words license.response['fetched_at'] %> ago)</dd>
                  <% end %>
                  <dt class="font-semibold text-sm">Cache store</dt>
                  <dd>
                    <%= Avo.cache_store.class %>
                  </dd>
                </dl>
              </div>
            </div>
          </div>
        </div>
        <div class="relative flex flex-col bg-white rounded shadow-panel p-4 space-y-4 h-full">
          <div class="flex justify-between w-full">
            <div class="font-semibold">Plugins</div>
          </div>
          <div class="flex flex-col flex-1">
            <div>
              <ul>
                <li>Avo <%= Avo::VERSION %></li>
                <% Avo.plugin_manager.plugins.each do |plugin| %>
                  <li><%= plugin.name %> - <%= plugin.version %></li>
                <% end %>
              </ul>
            </div>
          </div>
        </div>
      </div>
    <% end %>
  <% end %>
</div>
